﻿/// <reference path="jquery-1.6.4-vsdoc.js" />

jQuery.fn.value = function (value, round, dec) {
    /// <summary>
    ///     1: Pega o valor do elemento
    ///     <para>    1.1 - val()</para>
    ///     2: Seta o valor no elemento
    ///     <para>    2.1 - val(value) </para>
    ///     <para>    2.2 - val(value, round) </para>
    ///     <para>    2.1 - val(value, round, dec) </para>
    /// </summary>
    /// <param name="value" type="String">
    ///     Valor a ser convertido
    /// </param>
    /// <param name="round" type="Boolean">
    ///     Indica se está fará o arredondamento do valor (opcional, o valor default é false)
    /// </param>
    /// <param name="dec" type="Integer">
    ///     Número de casas decimais a ser mostrado (opcional, o valor default é 2)
    /// </param>
    /// <returns type="String" />
    if (value == undefined || value == null) {
        return moeda.desformatar(this[0].value);
    } else {
        if (typeof value == 'string') {
            value = moeda.desformatarFloat(value);
        }
        dec = dec == undefined || dec == '' ? 2 : dec;
        if (round) {
            value = moeda.arredondar(value, dec);
        }
        this[0].value = moeda.formatar(value.toString(), dec);
        return this;
    }
}
jQuery.fn.valueFloat = function () {
    /// <summary>
    ///     1: Pega o valor do elemento convertido em um float	
    /// <returns type="Float" />
    return moeda.desformatarFloat(this[0].value);
}
jQuery.fn.valuePorcent = function () {
    /// <summary>
    ///     1: Pega o valor do elemento convertido em um float	
    /// <returns type="Float" />
    return moeda.desformatarPorcent(this[0].value);
}
$('.integer').focus(function () {
    $(this).select();
});
$('.integer').keydown(function (event) {
    var e = event.keyCode;
    if ((e >= 96 && e <= 105) || (e >= 48 && e <= 57)) {
        return true;
    }
    switch (e) {
        default:
            return false;
            break;
        case 8: // backspace			
            return true;
            break;
        case 9: // tab			
            return true;
            break;
        case 13: // enter
            return true;
            break;
        case 35: // end
            return true;
            break;
        case 36: // home
            return true;
            break;
        case 37: // left arrow
            return true;
            break;
        case 39: // right arrow
            return true;
            break;
        case 46: // delete
            return true;
            break;
    }
});
$('.integer').live('blur', function () {
    var val = inteiro.desformatar(this.value);
    val = inteiro.formatar(val);
    $(this).val(val);
});
$('.decimal').focus(function () {
    $(this).select();
});
$('.decimal').keydown(function (event) {
    var e = event.keyCode;
    if (!navigator.systemLanguage == 'pt-BR') {
        e = e == 110 ? 194 : e;
    } else if (!navigator.language == 'pt-BR') {
        e = e == 110 ? 194 : e;
    }
    if ((e >= 96 && e <= 105) || (e >= 48 && e <= 57)) {
        return true;
    }
    switch (e) {
        default:
            return false;
            break;
        case 8: // backspace			
            return true;
            break;
        case 9: // tab			
            return true;
            break;
        case 13: // enter
            return true;
            break;
        case 35: // end
            return true;
            break;
        case 36: // home
            return true;
            break;
        case 37: // left arrow
            return true;
            break;
        case 39: // right arrow
            return true;
            break;
        case 46: // delete
            return true;
            break;
        //		case 109: // - ( teclado númerio )                       
        //			var val = $(this).val();                       
        //			val = val.toString().indexOf("-");                       
        //			if (val <= 0) {                       
        //				return true;                       
        //			} else {                       
        //				return false;                       
        //			}                       
        //			break;                       
        case 110: // , ( teclado númerio )
            var reg = new RegExp(/(^,$)|(^,[0-9]+$)|(^[0-9],?[0-9]+$)|(^[0-9]+,?$)/);
            var val = $(this).val();
            val = val + ',';
            return reg.test(val);
            break;
        case 188: // ,
            if ($(this).val().indexOf(",") <= 0) {
                return true;
            } else {
                return false;
            }
            break;
        //		case 189: // -                        
        //			var val = $(this).val();                       
        //			val = val.toString().indexOf("-");                       
        //			if (val <= 0) {                       
        //				return true;                       
        //			} else {                       
        //				return false;                       
        //			}                       
        //			break;                       
    }
});
$('.decimal').live('blur', function () {
    var val = this.value.replace('.', ',');
    val = moeda.desformatar(this.value);   
    val = moeda.formatar(val, 2);
    $(this).val(val);
});
$('.porcent').focus(function () {
    $(this).select();
});
$('.porcent').keydown(function (event) {
    var e = event.keyCode;
    var reg = new RegExp(/(^[1][0][0],[0]?[0]?[0]?$)|(^[1][0][0]$)|(^\d?\d,\d?\d?\d?$)|(^\d\d,\d?\d?\d?$)|(^,\d?\d?\d?$)|(^\d\d,\d?$)|(^\d?\d,?$)/);
    if (!navigator.systemLanguage == 'pt-BR') {
        e = e == 110 ? 194 : e;
    } else if (!navigator.language == 'pt-BR') {
        e = e == 110 ? 194 : e;
    }
    if ((e >= 48 && e <= 57) || (e >= 96 && e <= 105)) {
        var val = this.value.replace('.', ',');
        switch (e) {
            default:
                val = false;
                break;
            case 48:
                val = val + '0';
                break;
            case 49:
                val = val + '1';
                break;
            case 50:
                val = val + '2';
                break;
            case 51:
                val = val + '3';
                break;
            case 52:
                val = val + '4';
                break;
            case 53:
                val = val + '5';
                break;
            case 54:
                val = val + '6';
                break;
            case 55:
                val = val + '7';
                break;
            case 56:
                val = val + '8';
                break;
            case 57:
                val = val + '9';
                break;
            case 96:
                val = val + '0';
                break;
            case 97:
                val = val + '1';
                break;
            case 98:
                val = val + '2';
                break;
            case 99:
                val = val + '3';
                break;
            case 100:
                val = val + '4';
                break;
            case 101:
                val = val + '5';
                break;
            case 102:
                val = val + '6';
                break;
            case 103:
                val = val + '7';
                break;
            case 104:
                val = val + '8';
                break;
            case 105:
                val = val + '9';
                break;
        }
        return reg.test(val);
    }
    switch (e) {
        default:
            return false;
            break;
        case 8: // backspace			
            return true;
            break;
        case 9: // tab			
            return true;
            break;
        case 13: // enter
            return true;
            break;
        case 35: // end
            return true;
            break;
        case 36: // home
            return true;
            break;
        case 37: // left arrow
            return true;
            break;
        case 39: // right arrow
            return true;
            break;
        case 46: // delete
            return true;
            break;
        case 110: // , ( teclado númerio )			
            var val = $(this).val();
            val = val + ',';
            return reg.test(val);
            break;
        case 188: // ,
            if ($(this).val().indexOf(",") <= 0) {
                return true;
            } else {
                return false;
            }
            break;
    }
});
$('.porcent').live('blur', function () {
    var val = this.value.replace('.', ',');
    val = moeda.desformatar(val);
    val = moeda.formatar(val, 2);
    $(this).val(val);
});
var moeda = {
    desformatar: function (num) {
        if (num == undefined || num == '') {
            num = "0";
        } else {
            x = 0;
            if (parseFloat(num) < 0) {
                num = Math.abs(parseFloat(num)).toString(); x = 1;
            }
            while (num.match(/[.]/)) {
                num = num.replace(".", "");
            }
            if (x == 1) {
                num = '-' + num;
            }
        }
        return num;
    },
    formatar: function (num, digits) {
        digits = digits == undefined ? 2 : digits == '' ? 2 : digits;
        x = 0;
        if (parseFloat(num) < 0) {
            num = Math.abs(parseFloat(num)).toString(); x = 1;
        }
        num = num.replace(',', '.');
        var split = num.toString().split('.');
        num = split[0] == "" ? '0' : parseInt(split[0]).toString();
        cents = split[1] == undefined ? '0' : split[1];
        if (cents.length > digits) {
            cents = cents.substr(0, digits);
        } else if (cents.length < digits) {
            while (cents.length < digits) {
                cents = cents + '0';
            }
        }
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
            num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
        ret = num + ',' + cents;
        if (x == 1) {
            ret = '-' + ret;
        }
        return ret;
    },
    arredondar: function (num, dec) {
        return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    },
    desformatarFloat: function (num) {
        if (num == undefined || num == '') {
            num = "0";
        } else {
            while (num.match(/[.]/)) {
                num = num.replace(".", "");
            }
            num = num.replace(",", ".");
        }
        return parseFloat(num);
    }
}
var inteiro = {
    desformatar: function (num) {
        if (num == undefined) { num = "0"; } else if (num == '') { num = "0"; } else if (isNaN(num)) { num = "0"; } else { while (num.match(/[.]/)) { num = num.replace(".", ""); } }
        return parseInt(num);
    },
    formatar: function (num) {
        if (num == undefined) {
            num = "0";
        } else if (num == '') {
            num = "0";
        } else if (isNaN(num)) {
            num = "0";
        }
        num = num.toString();
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
            num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
        }
        return num;
    }
}
$(function () {
    $('input[tipo=integer]').each(function () {
        $(this).blur();
    });
    $('input[tipo=decimal]').each(function () {
        $(this).blur();
    });
    $('input[tipo=porcent]').each(function () {
        $(this).blur();
    });
});